package firstExercise;

import java.util.Scanner;

public class eight_1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int uNum = 4;
        eight_1_Users[] Users = new eight_1_Users[5];
        eight_1_Users st1 = new eight_1_Users(10001, "张三", "aaaaa");
        eight_1_Users st2 = new eight_1_Users(10002, "李四", "bbbbb");
        eight_1_Users st3 = new eight_1_Users(10003, "王五", "ccccc");
        eight_1_Users st4 = new eight_1_Users(10004, "赵六", "ddddd");
        Users[0] = st1;
        Users[1] = st2;
        Users[2] = st3;
        Users[3] = st4;
        //通过id查询用户姓名
        System.out.println(searchUser(Users, 10002));
        //通过id查询用户姓名
        System.out.println(searchUserPassword(Users, 10002));
        //输入账号、密码登录
        Boolean weather_register = new Boolean(false);
        while (!weather_register) {
            System.out.printf("登录uid:");
            int uid = sc.nextInt();
            System.out.printf("登录password:");
            String passWord = sc.next();
            if (register(Users, uid, passWord) == 0) {
                System.out.println("登录成功, 正在进入首页");
                weather_register = true;
            } else if (register(Users, uid, passWord) == 2) {
                System.out.println("密码错误, 请重新登录");
            } else if (register(Users, uid, passWord) == 1) {
                System.out.println("账号不存在, 并提示正在进入注册界面");
                while (true) {
                    System.out.printf("注册uid:");
                    int newUid = sc.nextInt();
                    System.out.printf("注册name:");
                    String newName = sc.next();
                    System.out.printf("注册password:");
                    String newPassWord = sc.next();
                    boolean flag = login(Users, newUid, newName, newPassWord);
                    if (flag == true) {
//                       weather_register=true;
                        System.out.println("注册成功，正在返回登录");
                        break;
                    }
                }
            }
        }
    }

    //创建注册的方法
    public static boolean login(eight_1_Users[] arr, int uid, String name, String password) {
        if (searchUser(arr, uid) != null) {
            System.out.println("该账号已被人使用，请重新注册");
            return false;
        } else {
            if (searchBlank(arr)) {//User数组没满的情况
                int index = searchUserNum(arr);
                eight_1_Users temp = new eight_1_Users(uid, name, password);
                arr[index] = temp;
                return true;
            } else {//数组满的情况
                eight_1_Users[] newArr = creatNewUser(arr);
                int index = searchUserNum(arr);
                newArr[index].setId(uid);
                newArr[index].setName(name);
                newArr[index].setPasswords(password);
                return true;
            }
        }
    }

    //创建全部账户输出方法
    public static void show(eight_1_Users[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.println(arr[i]);
            }
        }
    }

    //创建新数组的创建方法
    public static eight_1_Users[] creatNewUser(eight_1_Users[] arr) {
        eight_1_Users[] newArr = new eight_1_Users[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    //创建统计已注册的用户数量
    public static int searchUserNum(eight_1_Users[] arr) {
        int cnt = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                cnt++;
            }
        }
        return cnt;
    }

    //创建查找数组是否有空位的方法
    public static boolean searchBlank(eight_1_Users[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                return true;
            }
        }
        return false;
    }

    //创建登录的方法
    public static int register(eight_1_Users[] arr, int id, String password) {
        //先进行用户id验证
        if (searchUser(arr, id) != null) {
            //再进行密码验证
            if (password.equals(searchUserPassword(arr, id))) {
                return 0;
            } else {
                return 2;
            }
        } else {
            return 1;
        }
    }

    //创建查询用户的方法
    public static String searchUser(eight_1_Users[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && arr[i].getId() == id) {
                return arr[i].getName();
            }
        }
        return null;
    }

    //创建查询用户密码的方法
    public static String searchUserPassword(eight_1_Users[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && arr[i].getId() == id) {
                return arr[i].getPasswords();
            }
        }
        return null;
    }
}
